<script setup lang="ts">
import { reactive, ref, Ref } from 'vue'
import router from '@router'
import { useLocalStorage } from '@hooks/useLocalStorage'
import md5 from '@utils/md5'
import { SvgIcon } from '@base-comp'
import packageJson from '@utils/package'
import Service from '@apis/auth'
import { ElNotification } from 'element-plus'
import { loginData, loginResult, labelPositionType } from '#types/login'
import SignupModal from './SignupModal.vue'

const labelPosition: Ref<labelPositionType> = ref(labelPositionType.right)
const rules = reactive({
  loginName: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
})
const form: Ref<loginData> = ref({
  loginName: 'admin',
  password: 'admin@123'
})

localStorage.clear()
const loading: Ref<boolean> = ref(false)
const projectName: string = packageJson.name
const version: string = packageJson.version

const onLogin = async (): Promise<void> => {
  const password = md5(form.value.password)

  const data: loginResult = (await Service.login(
    Object.assign({}, form.value, { password })
  )) as loginResult
  useLocalStorage('user', data.user)
  useLocalStorage('token', data.token)
  router.push(data.menus[0].path).then(() => {
    ElNotification({
      title: '登录成功',
      message: '尊敬的' + data.user.userName + '，欢迎您登录本系统',
      type: 'success'
    })
    loading.value = false
  })
}

/* 注册弹窗 */
const signupModel = ref()
const showSignupModal = () => {
  signupModel.value.show()
}
</script>

<template>
  <div class="login-container">
    <img src="../../assets/bg.png" class="bg-img" />
    <div class="center">
      <div class="left">
        <img src="../../assets/bg_left.png" class="left-bg-img" />
      </div>

      <div class="form-wrapper">
        <div class="title">账号登录</div>
        <el-form
          :label-position="labelPosition"
          :model="form"
          label-width="20px"
          :rules="rules"
          style="margin-top: 30px"
        >
          <el-form-item prop="loginName">
            <el-input
              v-model="form.loginName"
              placeholder="请输入用户名"
              allow-clear
              size="large"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="form.password"
              placeholder="请输入密码"
              type="password"
              allow-clear
              size="large"
            />
          </el-form-item>
        </el-form>
        <div style="flex: 1" />
        <el-button type="primary" class="login" :loading="loading" @click="onLogin">
          登录
        </el-button>
        <div style="flex: 1; margin-top: 10px">
          <div class="flex justify-between">
            <el-checkbox>自动登录</el-checkbox>
            <el-col :span="6" style="text-align: right">
              <el-link :underline="false" type="primary" @click="showSignupModal">注册</el-link>
              <el-divider direction="vertical" />
              <el-link :underline="false" type="info">忘记密码</el-link>
            </el-col>
          </div>
        </div>
        <el-divider>
          <span style="color: #939393">第三方登录</span>
        </el-divider>
        <div class="flex justify-around">
          <SvgIcon iconName="weixin-copy" color="#19a537" :size="30" />
          <SvgIcon iconName="github" color="#234432" :size="30" />
          <SvgIcon iconName="sr_zhifubao" color="#0095e0" :size="30" />
        </div>
      </div>
      <div class="bottom">
        {{
          'Vue3 + Vite + Typescript + Trobl Admin © ' +
          projectName +
          ' ' +
          version +
          ' · Made by jinzz'
        }}
      </div>
    </div>
    <signup-modal ref="signupModel" />
  </div>
</template>

<style lang="scss" scoped>
:deep(.el-divider__text) {
  background-color: #fff;
}
.login-container {
  position: relative;
  overflow: hidden;
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;

  .bg-img {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
  }

  .bottom {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 3%;
    font-size: 14px;
    font-weight: bold;
    color: #333;
    text-align: center;
  }

  .center {
    position: relative;
    z-index: 9;
    width: 70%;
    height: 60%;
    border-radius: 10px;
    border: 1px solid #f5f5f5;
    display: flex;
    align-items: center;
    background-color: #fff;
    box-shadow: 0 0 5px #ececec;

    .left {
      position: relative;
      width: 50%;
      height: 100%;
      padding: 20px;
      padding-left: 0px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      text-align: center;

      .left-bg-img {
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
      }
    }

    .form-wrapper {
      width: 50%;
      padding: 2% 5%;
      max-height: 100%;
      min-height: 100%;
      display: flex;
      box-sizing: border-box;
      flex-direction: column;

      .title {
        font-size: 25px;
        font-weight: bold;
        margin-bottom: 20px;
        text-align: center;
        color: #000;
      }

      .login {
        width: 100%;
      }
    }
  }
}

.flex {
  width: 100%;
  display: flex;
}
.justify-between {
  justify-content: space-between;
}
.justify-around {
  justify-content: space-around;
}
.el-form-item__content {
  margin-left: 0px !important;
}


:deep(.el-checkbox__label) {
  color: rgb(51,51,51);
}
:deep(.el-input__inner) {
  color: rgb(51,51,51)
}
</style>
